package util;

import org.encog.ml.data.basic.BasicMLData;
import org.encog.neural.networks.BasicNetwork;
import org.encog.neural.rbf.RBFNetwork;

public class Erro {
	
	public static double calcularErroMedioPercentualAbs(BasicNetwork net,
			double[][] in, double[][] out) {
		double sum = 0;
		int n = 0;
		double[] previsao = new double[out[0].length];

		for (int i = 0; i < in.length; i++) {
			net.compute(in[i], previsao);

			for (int j = 0; j < previsao.length; j++) {
				// System.out.println(Math.abs(previsao[j]-out[i][j])/out[i][j]);
				sum += (Math.abs(out[i][j] - previsao[j]) / out[i][j]);
				n++;
			}

		}

		return (100 * sum / n);
	}

	public static double calcularErroMedioQuadrado(BasicNetwork net, double[][] in,
			double[][] out) {
		double sum = 0;
		int n = 0;
		double[] observado = new double[out[0].length];

		for (int i = 0; i < in.length; i++) {
			net.compute(in[i], observado);

			for (int j = 0; j < observado.length; j++) {
				sum += (Math.pow(out[i][j] - observado[j], 2));
				n++;
			}

		}

		return (sum / n);
	}
	
	public static double calcularErroMedioPercentualAbs(RBFNetwork net,
			double[][] in, double[][] out) {
		
		double sum = 0;
		int n = 0;
		double[] previsao = new double[out[0].length];

		for (int i = 0; i < in.length; i++) {
			previsao = net.compute(new BasicMLData(in[i])).getData();

			for (int j = 0; j < previsao.length; j++) {
				// System.out.println(Math.abs(previsao[j]-out[i][j])/out[i][j]);
				sum += (Math.abs(out[i][j] - previsao[j]) / out[i][j]);
				n++;
			}

		}

		return (100 * sum / n);
	}
}
